package com.songcha.sakura.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.songcha.sakura.R
import com.songcha.sakura.bean.HomeBannerListBean
import com.songcha.library_common.ui.view.loading.FlowerLoadingView
import com.songcha.sakura.util.MyJumpUtil
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

class HomeBannerAdapter(val mContext: Context) : BaseBannerAdapter<HomeBannerListBean.HomeBannerBean>() {

    override fun bindData(holder: BaseViewHolder<HomeBannerListBean.HomeBannerBean>, data: HomeBannerListBean.HomeBannerBean, position: Int, pageSize: Int) {
        val iv: ImageView = holder.findViewById(R.id.iv_banner)
        val tv: TextView = holder.findViewById(R.id.tv_title)
        val flv: FlowerLoadingView = holder.findViewById(R.id.flv)
        if(data.img.isNotEmpty())
            Glide.with(mContext).load(data.img).listener( object :RequestListener<Drawable>{
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    flv.visibility= View.GONE
                    return false
                }

            }).into(iv)
        iv.setOnClickListener {
            MyJumpUtil.toSearchComic(mContext,data.title)
        }
        tv.text=data.title
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_home_banner
    }

}